package com.muy.redis.view.window.redis.view.table;

import com.intellij.ui.ColoredTableCellRenderer;
import com.muy.redis.utils.JacksonUtils;

import javax.swing.*;

/**
 * @Author jiyanghuang
 * @Date 2022/8/3 23:35
 */
public class MRTableCellRenderer extends ColoredTableCellRenderer {

    /**
     * @param table    表格对象
     * @param value    某个方格的值,可以根据不同的类型拼接
     * @param selected 当前是否被选中
     * @param hasFocus 是否获取焦点
     * @param row      行 0 开始
     * @param column   列 0 开始
     */
    @Override
    protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {

        if (value == null) {
            append("null", StyleAttributesProvider.getNullAttribute());
        } else {
            if (value instanceof Number) {
                append(String.valueOf(value), StyleAttributesProvider.getNumberAttribute());
            } else if (value instanceof Boolean) {
                append(String.valueOf(value), StyleAttributesProvider.getBooleanAttribute());
            } else if (value instanceof ColumnDesc) {
                ColumnDesc val = (ColumnDesc) value;
//                System.out.println("row:" + row + " column:" + column + " value:" + String.valueOf(val.getValueShow()));
                if (null != val.getColumnShowAttr() && !val.getColumnShowAttr().equals(ColumnShowAttr.NULL)) {
                    append(String.valueOf(val.getValueShow()), val.getColumnShowAttr().getSimpleTextAttributes());
                } else {
                    if (JacksonUtils.jsonStr(val.getValueShow())) {
                        append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getKeyValueAttribute());
                    } else {
                        append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getStringAttribute());
                    }
                }
            } else {
                append(String.valueOf(value), StyleAttributesProvider.getStringAttribute());
            }
        }
    }
}
